﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;

namespace InsPipeInfo
{
    public class Excel
    {
        public ISheet GetSheet(string ExcelPath)
        {

            string newFileName = Guid.NewGuid().ToString() + ".xlsx";
            File.Copy(ExcelPath, newFileName);
            ISheet sheet;

            using (FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read))
            {
                IWorkbook workbook = WorkbookFactory.Create(fs);
                sheet = workbook.GetSheetAt(0);
            }
            Console.WriteLine("读取Excel成功");
            File.Delete(newFileName);
            return sheet;
        }

        public void ReadSheet(ISheet sheet)
        {
            Dictionary<string, InfGroup> serviceGroup = new Dictionary<string, InfGroup>();


            for (int i = 1; i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                InfObj infObj = new InfObj(row);
                if (serviceGroup.ContainsKey(infObj.INST_HPR.Substring(1, 3)))
                {
                    serviceGroup[infObj.INST_HPR.Substring(1, 3)].Add(infObj);

                }
                else
                {
                    serviceGroup.Add(infObj.INST_HPR.Substring(1, 3), new InfGroup(infObj.INST_HPR.Substring(1, 3)));
                    serviceGroup[infObj.INST_HPR.Substring(1, 3)].Add(infObj);
                }


            }
            foreach (InfGroup group in serviceGroup.Values)
            {
                group.Write();
            }

        }

        internal void ReadSheet(ISheet sheet)
        {
            throw new NotImplementedException();
        }
    }
}
